/* 列表的封装 */

// 列表数据 分页 搜索 删除 修改状态
import {
    reactive,
    ref
} from "vue"
import {
    Toast
} from '@/utils/utils';
export const useInitTable = (opt = {}) => {
    let tableList = ref([])
    let page = ref(1)
    let total = ref(0)
    let limit = ref(10)

    let searchForm = null
    let resetSearchForm = null
    if (opt.searchForm) {
        searchForm = reactive({
            ...opt.searchForm
        })
        resetSearchForm = () => {
            for (const key in opt.searchForm) {
                searchForm[key] = opt.searchForm[key]
            }
            getData()
        }
    }
    // 获取数据
    const getData = async () => {
        let res = await opt.getList({
            page: page.value,
            limit: limit.value,
            ...searchForm
        })
        if (res.status == 200) {
            if (opt.onGetListSuccess && typeof opt.onGetListSuccess == "function") {
                opt.onGetListSuccess(res)
            } else {
                total.value = res.data.totalCount
                tableList.value = res.data.list
            }
        }
    }
    getData()
    // 分页
    const changePage = (e) => {
        page.value = e
        getData()
    }

    // 删除
    const handleDelete = (id) => {
        opt.del(id).then(res => {
            if (res.status == 200) {
                Toast('删除成功')
                getData()
            }
        })
    }

    // 修改状态
    const statusChange = (item) => {
        opt.status(item.id, {
            status: Number(!item.status)
        }).then(res => {
            if (res.data) {
                Toast('修改状态成功')
            }
        })
    }
    //  刷新
    const refresh = () => {
        page.value = 1
        limit.value = 10
        getData()
    }
    // 批量删除
    let ids = ref([]);
    const handleDeleteAll = (e) => {
        ids.value = e.map((o) => o.id);
    };
    const delAllClick = () => {
        opt.del(ids.value).then((res) => {
            if (res.status == 200) {
                Toast("删除成功");
                getData();
            }
        });
    };
    // 批量上架下架
    const changeAllClick = (status) => {
        opt.changeAll(status, ids.value).then((res) => {
            if (res.status == 200) {
                Toast(status ? '上架成功' : '下架成功');
                getData();
            }
        });
    };
    return {
        tableList,
        page,
        total,
        limit,
        searchForm,
        getData,
        resetSearchForm,
        changePage,
        statusChange,
        handleDelete,
        refresh,
        ids,
        handleDeleteAll,
        delAllClick,
        changeAllClick
    }
}
// 添加修改
export const useInitForm = (opt = {}) => {
    // 新增
    const fromData = reactive({})
    const defaultForm = opt.fromData
    const editId = ref(0)
    const title = ref("add")
    const formRef = ref()
    const rules = opt.rules || {}
    const DrawerRef = ref()
    // 新增
    const createClick = () => {
        title.value == 'add'
        resetForm(defaultForm)
        DrawerRef.value.openDrawer()
    }

    // 重置表单
    function resetForm(row = false) {
        if (formRef.value) formRef.value.clearValidate()
        for (const key in defaultForm) {
            fromData[key] = row[key]
        }
    }
    // 提交
    const submit = () => {
        formRef.value.validate((valid) => {
            if (!valid) return
            let body = {}
            if (opt.beforeSubmit && typeof opt.beforeSubmit == "function") {
                body = opt.beforeSubmit({ ...fromData })
            } else {
                body = fromData
            }
            if (title.value === 'add') {
                opt.create(body).then(res => {
                    if (res.status == 200) {
                        Toast('新增成功')
                        DrawerRef.value.closeDrawer()
                        opt.getData()
                    }
                })
            } else {
                opt.updata(editId.value, body).then(res => {
                    if (res.status == 200) {
                        Toast('修改成功')
                        DrawerRef.value.closeDrawer()
                        opt.getData()

                    }
                })
            }
        })
    }

    // 修改
    const editNoticeClick = (row) => {
        DrawerRef.value.openDrawer()
        editId.value = row.id
        resetForm(row)
        title.value = 'edit'
    }

    return {
        fromData,
        defaultForm,
        editId,
        title,
        formRef,
        rules,
        DrawerRef,
        createClick,
        resetForm,
        submit,
        editNoticeClick
    }
}